package 笔试.小米;

import java.util.*;

public class Two {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        int a = scanner.nextInt();
        int b = scanner.nextInt();
        scanner.close();

        int result = minMagicSteps(a, b);
        System.out.println(result);
    }

    private static int minMagicSteps(int a, int b) {
        if (a == b) {
            return 0;
        }

        Queue<State> queue = new LinkedList<>();
        Set<Integer> visited = new HashSet<>();
        queue.add(new State(1, 0));
        visited.add(1);

        while (!queue.isEmpty()) {
            State current = queue.poll();
            int value = current.value;
            int steps = current.steps;
            int newValue = value * a;
            if (newValue == b) {
                return steps + 1;
            }
            if (newValue < 1_000_000 && !visited.contains(newValue)) {
                queue.add(new State(newValue, steps + 1));
                visited.add(newValue);
            }
            if (value >= 10 && value % 10 != 0) {
                newValue = rotateRight(value);
                if (newValue == b) {
                    return steps + 1;
                }
                if (newValue < 1_000_000 && newValue != value && !visited.contains(newValue)) {
                    queue.add(new State(newValue, steps + 1));
                    visited.add(newValue);
                }
            }
        }

        return -1;
    }

    private static int rotateRight(int n) {
        String str = Integer.toString(n);
        str = str.charAt(str.length() - 1) + str.substring(0, str.length() - 1);
        return Integer.parseInt(str);
    }

    static class State {
        int value;
        int steps;

        State(int value, int steps) {
            this.value = value;
            this.steps = steps;
        }
    }
}